package net.lesscoding.common;

import cn.hutool.core.util.StrUtil;
import net.lesscoding.protocol.Serializer;

import java.io.InputStream;
import java.util.Properties;

/**
 * @author eleven
 * @date 2024/12/2 17:43
 * @apiNote
 */
public abstract class Config {
    static Properties properties;

    static {
        try(InputStream in = Config.class.getClassLoader().getResourceAsStream("application.properties")) {
            properties = new Properties();
            properties.load(in);
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public static int getServerPort() {
        String port = properties.getProperty("server.port");
        if (StrUtil.isBlank(port)) {
            return 1024;
        } else {
            return Integer.parseInt(port);
        }
    }

    public static Serializer.Algorithm getSerializerAlgorithm() {
        String property = properties.getProperty("serializer.algorithm");
        if (StrUtil.isBlank(property)) {
            return Serializer.Algorithm.JAVA;
        }
        return Serializer.Algorithm.valueOf(property);
    }
}
